<?php


namespace app\platform\merchant\controller;


use app\common\controller\BaseController;
use app\common\model\SysMerchantMenu;
use support\Response;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;

/**
 * 管理租户端菜单
 * Class MerchantMenuController
 * @package app\platform\system\controller
 */
class MerchantMenuController extends BaseController
{
    protected $model = SysMerchantMenu::class;

    protected array $noNeedRight = [
        'listRouter',
    ];

    public function list()
    {
        $name = input('name', '');
        $where = [];
        $name && $where[] = ['name', 'like', "%{$name}%"];

        $data = $this->getModel()
            ->where($where)
            ->select();
        return $this->success($data);
    }

    public function add()
    {
        $data = request()->all();
        $this->getModel()->insert($data);
        return $this->success();
    }

    public function update()
    {
        $data = request()->all();
        $this->getModel()->where('menuId', '=', $data['menuId'])->update($data);
        return $this->success();
    }

    public function deleteById()
    {
        $id = input('id');
        $this->getModel()->where('menuId', '=', $id)->delete();
        return $this->success();
    }

    public function batchDelete()
    {
//        $ids = input('ids');
//        $this->getModel()->where('menuId', 'in', $ids)->delete();
        return $this->success();
    }

    public function getById()
    {
        $id = input('id');
        $data = $this->getModel()->where('menuId', '=', $id)->find();
        return $this->success($data);
    }

    /**
     * 获取正常的菜单
     * @return Response
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function listMenuNormal()
    {
        $where = [
            ['menuStatus', '=', 0]
        ];
        $data = $this->getModel()
            ->where($where)
            ->select()
            ->toArray();
        $menuIds = array_column(array_filter($data, function ($v) {
            return $v['isSpread'] == 0;
        }), 'menuId');
        return $this->success([
            'menuList' => $data,
            'spreadList' => $menuIds,
        ]);
    }

    /**
     * 获取角色已分配的菜单
     * @return Response
     */
    public function listMenuIdsByRoleId()
    {
        // 菜单反选数据[菜单反选只注重子节点，父节点不用返回，子节点都有父节点自然会选中]
        $id = input('id', 0);
        $data = $this->getModel()->alias('a')
            ->join('sys_role_menu b', 'a.menuId=b.menu_id')
            ->where('b.role_id', '=', $id)
            ->where('b.menuStatus', '=', 0)
            ->order("a.sorted")
            ->field([
                'a.menuId',
                'a.parentId',
            ])
            ->select()
            ->toArray();
        $res = [];
        $hasChild = $this->getModel()
            ->whereIn('parentId', array_column($data, 'menuId'))
            ->column('parentId');
        foreach ($data as $datum) {
            if (!in_array($datum['menuId'], $hasChild)) {
                $res[] = $datum['menuId'];
            }
        }
        return $this->success($res);
    }


    public function cascaderList()
    {
        $where = [
            ['menuType', 'in', [1, 2]],
            ['menuStatus', "=", 0],
        ];
        $data = $this->getModel()
            ->where($where)
            ->field([
                'parentId',
                'menuName as label',
                'menuId as value',
            ])
            ->select();
        return $this->success($data);
    }

    public function updateSpread()
    {
        $id = input('id');
        $spread = input('spread', 0);
        $data = $this->getModel()->where('menuId', '=', $id)->update([
            'isSpread' => $spread
        ]);
    }
}